package com.zt.rongda.asset.controller;

import com.zt.servlet.*;
import com.zt.rongda.asset.model.Asset;
import com.zt.rongda.asset.service.AssetService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/asset")
public class AssetController {

    @Autowired
    private AssetService assetService;

    @RequestMapping(value = "/query")
    @ResponseBody

    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(assetService.getTable(reqBean));
    }

    @RequestMapping(value = "/queryType")
    @ResponseBody

    public JSONObject queryType(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(assetService.queryType(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetService.add(getAsset(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetService.update(getAsset(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        assetService.delete(reqBean.getInfo("assetId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private Asset getAsset(ReqBean reqBean) {
        Asset asset = new Asset();
        asset.setAssetId(reqBean.getInfo("assetId"));
        asset.setAssetCode(reqBean.getInfo("assetCode"));
        asset.setAssetName(reqBean.getInfo("assetName"));
        asset.setAssetClass(reqBean.getInfo("assetClass"));
        asset.setType(reqBean.getInfo("type"));
        asset.setModel(reqBean.getInfo("model"));
        asset.setNum(reqBean.getInfoInt("num"));
        asset.setPurchaseDate(reqBean.getInfoDate("purchaseDate"));
        asset.setUserId(reqBean.getInfo("userId"));
        asset.setUserName(reqBean.getInfo("userName"));
        asset.setDeptId(reqBean.getInfo("deptId"));
        asset.setDeptName(reqBean.getInfo("deptName"));
        asset.setLocation(reqBean.getInfo("location"));
        asset.setUseDate(reqBean.getInfo("useDate"));
        asset.setOperator(reqBean.getUserId());
        asset.setRemark(reqBean.getInfo("remark"));
        asset.setAssetOld(reqBean.getInfo("assetOld"));
        return asset;
    }

}